package com.ax.api.config.upload;

import com.ax.api.config.AjaxRes;
import com.ax.api.config.upload.model.CosClientEntity;
import com.ax.api.config.upload.model.YmUpload;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class UploadOneController{
    private static final String basePath = CosClientEntity.realmName + File.separator;

    /**
     * 上传单个文件
     * @param imageFile
     * @return
     */
    @RequestMapping("/web/uploadHeader")
    public AjaxRes upload(@RequestBody MultipartFile imageFile) {
        YmUpload ymUpload = new YmUpload();
        AjaxRes ajaxRes = new AjaxRes();
        try {
            String imageFilePath = FileUtil.upload(imageFile);
            if (StringUtils.isNotBlank(imageFilePath)) {
                ymUpload.setImagePath(basePath + imageFilePath);
            }
            ajaxRes.setSucceed(ymUpload);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ajaxRes;
    }

}
